﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;

namespace Fonlow.CommandLineGui.Gui
{
    internal class TextEditorWindow : IDisposable
    {
        public Form Window { get; private set; }
        public RichTextBox TextBox { get { return textBox; } }

        RichTextBox textBox;

        public TextEditorWindow()
        {
            Window = new Form();
            textBox = new RichTextBox(); // RichText is good for checking the cursor position through the SelectionChanged event.
            textBox.Dock = DockStyle.Fill;
            textBox.Multiline = false;
            textBox.WordWrap = true;
            textBox.Font = new Font("Courier New", 10, FontStyle.Regular);
            textBox.DetectUrls = false;


            Window.Text = "Command";
            Window.ShowInTaskbar = false;
            Window.Controls.Add(textBox);

            textBox.SelectionChanged += new EventHandler(textBox_SelectionChanged);
        }

        /// <summary>
        /// Telling the world what parameter selected through the WordSelected event being fired.
        /// </summary>
        void textBox_SelectionChanged(object sender, EventArgs e)
        {
            int selectionStart = textBox.SelectionStart;
            if (selectionStart == textBox.Text.Length)
                selectionStart--;

            string parameterText = null;

            if (CursorPositionChanged != null)
            {
                ParameterHighlightedEventArgs args = new ParameterHighlightedEventArgs(textBox.Text, selectionStart);
                CursorPositionChanged(this, args);//so the event handler will assign args.Parameter.
                parameterText = args.Parameter;
                if (String.IsNullOrEmpty(parameterText))
                    return;

                if (WordSelected != null)
                {
                    WordSelected(this, new TextMessageEventArgs(parameterText));
                }
            }


        }

        /// <summary>
        /// Fired when mouse clicking on a parameter in the commandline.
        /// </summary>
        public event EventHandler<TextMessageEventArgs> WordSelected;

        /// <summary>
        /// Fired when mouse clicking anywhere in the commandline. This event
        /// is fired before WordSelected, and may or may not fire WordSelected event.
        /// </summary>
        public event EventHandler<ParameterHighlightedEventArgs> CursorPositionChanged;


        #region IDisposable Members
        bool disposed;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "textBox")]
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    Window.Dispose();
                }
                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }

    internal class OutputWindow : IDisposable
    {
        public OutputWindow()
        {
            Window = new Form();
            textBox = new TextBox();
            textBox.Dock = DockStyle.Fill;
            textBox.Multiline = true;
            textBox.WordWrap = true;
            textBox.ScrollBars = ScrollBars.Vertical;
            textBox.Font = new Font("Courier New", 9, FontStyle.Regular);

            Window.Text = "Output";
            Window.ShowInTaskbar = false;
            Window.Controls.Add(textBox);
        }

        public Form Window { get; private set; }
        public TextBox TextBox { get { return textBox; } }

        TextBox textBox;

        #region IDisposable Members
        bool disposed;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "textBox")]
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    Window.Dispose();
                }
                disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
